package com.qk.service.impl;

import com.qk.mapper.BusinessMapper;
import com.qk.mapper.ClueMapper;
import com.qk.service.ReportService;
import com.qk.vo.OverviewVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Slf4j
@Service
public class ReportServiceImpl implements ReportService {

    @Autowired
    private ClueMapper clueMapper;

    @Autowired
    private BusinessMapper businessMapper;

    @Autowired
    private RedisTemplate<Object, Object> redisTemplate;

    @Override
    public OverviewVo getOverview() {
        //查询Redis缓存中的数据
        log.info("从Redis中查询显示和商机的概览数据");
        Object overview = redisTemplate.opsForValue().get("overview");

        if (overview != null){
            log.info("从Redis中,获取线索和商机的概览数据");
            return (OverviewVo) overview;
        }

        //获取线索对应的数据
        OverviewVo clueOverride = clueMapper.getClueOverview();
        //获取商机对应数据
        OverviewVo businessOverride = businessMapper.getBusinessOverview();
        //封装OverviewVo
        BeanUtils.copyProperties( clueOverride, businessOverride,"businessTotal","businessWaitAllot","businessWaitFollow","businessFollowing","businessFalse","businessConvertCustomer");
        //缓存数据
        log.info("将数据缓存到Redis中");
        redisTemplate.opsForValue().set("overview", businessOverride, 30, TimeUnit.MINUTES);

        return businessOverride;
    }
}
